﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpainter.h"
#include "qevent.h"
#include "qpoint.h"
#include "qmessagebox.h"





MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	ui->mainToolBar->hide();
	ui->menuBar->hide();

    resize((CHESS_COLUMES + 1)*RECT_WIDTH  ,(CHESS_ROWS + 1)*RECT_HEIGHT);//页面大小

	mIsBlackTurn = true;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *e)
{
    //所有的绘图事件集合
	DrawChessboard();		//画棋盘
	DrawItems();			//画棋子
	DrawItemWithMouse();	//画鼠标（当前方的棋子形状）

    update();//页面更新
}

void MainWindow::DrawChessboard()
{
    //画棋盘
	QPainter painter(this);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true); //保证线条清晰
    painter.setBrush(Qt::yellow);
    painter.setPen(QPen(QColor(Qt::black),2));//设置画笔
    //循环画格子
	for(int i = 0;i<CHESS_COLUMES; i++)
	{
		for (int j = 0; j<CHESS_ROWS; j++)
		{
			painter.drawRect( (i+0.5)*RECT_WIDTH,(j+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
		}
	}
}

void MainWindow::DrawItems()
{
    //画棋盘棋子
	QPainter painter(this);
    //使棋子的边线透明
	painter.setPen(QPen(QColor(Qt::transparent)));
    //遍历棋子，根据棋子bool值来判断颜色
	for (int i = 0; i<mItems.size(); i++)
	{
        Item item = mItems[i];//当前棋子的样式位置赋给棋子这个类
		if (item.mBlack)
		{
			painter.setBrush(Qt::black);
		}
		else
		{
			painter.setBrush(Qt::white);
		}
		DrawChessAtPoint(painter,item.mPt);
	}
}

void MainWindow::DrawChessAtPoint(QPainter& painter,QPoint& pt)
{
     //获取棋子的摆放位置

    QPoint ptCenter((pt.x()+0.5)*RECT_WIDTH,(pt.y()+0.5)*RECT_HEIGHT);//获取圆心
    painter.drawEllipse(ptCenter,RECT_WIDTH / 2,RECT_HEIGHT / 2);//画棋子

}

void MainWindow::DrawItemWithMouse()
{
    //画鼠标棋子
	QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));//使棋子的边线透明
    //根据棋子bool值来判断颜色
	if (mIsBlackTurn)
	{
		painter.setBrush(Qt::black);
	}
	else
	{
		painter.setBrush(Qt::white);
	}

                        //接受鼠标位置⬇
	painter.drawEllipse(mapFromGlobal(QCursor::pos()),RECT_WIDTH / 2,RECT_HEIGHT / 2);
	
}

void MainWindow::mousePressEvent(QMouseEvent * e)
{
	//求鼠标点击处的棋子点pt
	QPoint pt;
	pt.setX( (e->pos().x() ) / RECT_WIDTH);
	pt.setY( (e->pos().y() ) / RECT_HEIGHT);

    //判断棋子是否存在
	for (int i = 0; i<mItems.size(); i++)
	{
		Item item = mItems[i];
		if (item.mPt == pt)
		{
            //存在则跳过
			return;
		}
	}
    //不存在则落子
	Item item(pt,mIsBlackTurn);
	mItems.append(item);

    //核心算法，判断是否五连（判断应在落子后）
	int nLeft =			CountNearItem(item,QPoint(-1,0));
	int nLeftUp =		CountNearItem(item,QPoint(-1,-1));
	int nUp =			CountNearItem(item,QPoint(0,-1));
	int nRightUp =		CountNearItem(item,QPoint(1,-1));
	int nRight =		CountNearItem(item,QPoint(1,0));
	int nRightDown =	CountNearItem(item,QPoint(1,1));
	int nDown =			CountNearItem(item,QPoint(0,1));
	int nLeftDown =		CountNearItem(item,QPoint(-1,1));
	if ( (nLeft + nRight) >= 4 ||
		 (nLeftUp + nRightDown) >= 4 ||
		 (nUp + nDown) >= 4 ||
		 (nRightUp + nLeftDown) >= 4 )
	{
        QString str = mIsBlackTurn?"黑棋胜利":"白棋胜利";
        QMessageBox::information(NULL,  "游戏结束",str, QMessageBox::Yes );
		mItems.clear();
        //清空棋盘
		return;
	}
	//该另一方下棋了
	mIsBlackTurn = !mIsBlackTurn;
}

int MainWindow::CountNearItem(Item item,QPoint ptDirection)
{
	int nCount = 0;
	item.mPt += ptDirection;

	while (mItems.contains(item))
	{
		nCount++;
		item.mPt += ptDirection;
	}
	return nCount;
}


